package com.powerunion.datacollection.service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.common.SystemException;
import com.powerunion.datacollection.dao.DaoFactory;
import com.powerunion.datacollection.dao.TargetsCategoryDao;
import com.powerunion.datacollection.model.TargetsCategory;
import com.powerunion.util.log.LogHelper;

public class TargetsCategoryManager {
	private static Logger logger = LogHelper.getInstance().getLogger(TargetsCategoryManager.class);
	private TargetsCategoryDao dao = null;
	public TargetsCategoryManager() {
		super();
		this.dao = (TargetsCategoryDao)DaoFactory.getInstance().getDao(TargetsCategoryDao.class);
	}
	
	public String save(String saveStr) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(saveStr)) {
			String[] args = { "saveStr" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter saveStr NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		JSONObject jsonObject = JSONObject.fromObject(saveStr);
		TargetsCategory tc = (TargetsCategory)JSONObject.toBean(jsonObject,TargetsCategory.class);

		return this.dao.save(tc);
		
	}
	
	public boolean delete(String[] codes) {
		//参数为空
		if(codes.length < 1) {
			String[] args = { "codes" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter codes NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.delete(codes);
	}
	
	public TargetsCategory getBykey(String id) {
		//参数为空
		if(StringUtils.isEmpty(id)) {
			String[] args = { "id" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter id NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getBykey(id);
	}
	
	public JSONArray getTree(String paraSeq,int flag) throws Exception {
		//参数为空
		if(StringUtils.isEmpty(paraSeq)) {
			String[] args = { "paraSeq" };
			// 打印错误日志
			StringBuffer msglog = new StringBuffer(
					"empty invoke parameter paraSeq NULL ");
			logger.error(msglog.toString());
			throw new SystemException("error.common.parameter.empty", args);
		}
		
		return this.dao.getTree(paraSeq,flag);
	}
}
